package com.ynny.usermanager.controller;

import com.ynny.usermanager.dao.UserDao;
import com.ynny.usermanager.entity.R;
import com.ynny.usermanager.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserDao userDao;


    @GetMapping("/{id}")
    public R get(@PathVariable(name = "id") Integer id){

        System.out.println("查询用户ID："+id);

        User user = userDao.selectUserById(id);


        return R.ok(user);
    }



    @GetMapping("/list")
    public R list(){

        List<User> users = userDao.selectUsers();

        return R.ok(users);
    }



    @PostMapping("/add")
    public R add(@RequestBody User user){

        //模拟添加用户信息
        System.out.println("添加用户信息："+user);

        int i = userDao.insertUser(user);
        if (i > 0){
            return R.ok(user);
        }

        return R.ok(null);

    }


    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable(name = "id") Integer id ){

        System.out.println("删除用户ID："+id);
        int i = userDao.deleteUserById(id);
        if (i > 0){
            return R.ok(id);
        }

        return R.ok(null);
    }


    @PutMapping("/update")
    public R update(@RequestBody User user){

        System.out.println("更新用户信息："+user);

        int i = userDao.updateUser(user);
        if (i <= 0){
            return R.ok(null);
        }

        return R.ok(user);
    }

}
